/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukumagic.
 *
 * jukumagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukumagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukumagic.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "project.h"
#include "ratsnest.h"
#include <iostream>
#include "jukuapp.h"

/*
	Private methods
*/

void Project::ChildChanged(ObjectChangeType type, JukuObject *obj)
{
	if(m_board && obj == m_board && type == OBJECT_CHANGE_CONTENTS){
		std::cerr<<"Project::ChildChanged(): board changed"<<std::endl;
		if(wxGetApp().m_settings.m_automatic_rats_nest){
			std::cerr<<"Project::ChildChanged(): Automatic Rat's Nest activated"<<std::endl;
			CreateRatsNest(false);
		}
	}
}

/*
	Public methods
*/

Project::Project()
{
	//std::cout<<"Project"<<std::endl;
	m_board = NULL;
	//m_netlist = new Netlist();
	m_netlist.m_owner = this;
}

Project::~Project()
{
	//std::cout<<"~Project"<<std::endl;
	if(m_board) delete m_board;
	m_board = NULL;
	m_netlist.m_owner = NULL;
	//delete m_netlist;
}

void Project::WriteXML(TiXmlNode* root)
{
	TiXmlElement *element = new TiXmlElement("Project");
	root->LinkEndChild(element);
	//element->SetAttribute("name", m_name.mb_str(wxConvUTF8));
	//element->SetDoubleAttribute("scale", m_scale);

	m_board->WriteXML(element);
	//m_netlist->WriteXML(element);
	m_netlist.WriteXML(element);

	WriteBaseXML(element);
}

JukuObject* Project::ReadFromXMLElement(TiXmlElement* element)
{
	Project *project = new Project();

	//project->m_name = wxString(element->Attribute("name"), wxConvUTF8);

	//element->Attribute("scale", &project->m_scale);

	project->m_board =
			(Board*)Board::ReadFromXMLElement(element->FirstChildElement("Board"));
	project->m_board->m_owner = project;
	/*project->m_netlist =
			(Netlist*)Netlist::ReadFromXMLElement(element->FirstChildElement("Netlist"));
	project->m_netlist->m_owner = project;*/
	project->m_netlist.ReadBaseXML(element->FirstChildElement("Netlist"));

	project->ReadBaseXML(element);
	
	return project;
}

//

void Project::CreateRatsNest(bool only_selected)
{
	if(!m_board) return;
	//if(!m_netlist) return;
	m_board->m_ratsnest->Create(m_board, &m_netlist, only_selected);
}

void Project::CreateInvRatsNest(bool only_selected)
{
	if(!m_board) return;
	//if(!m_netlist) return;
	m_board->m_ratsnest->CreateInv(m_board, &m_netlist, only_selected);
}


